package top.xxyu;

import com.jcraft.jsch.SftpException;
import top.xxyu.utils.SFTPUtil;
import top.xxyu.utils.SSHGanymedUtil;
import top.xxyu.utils.SftpClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.regex.Matcher;

public class SSHDemo {


    /**
     *
     ;down;132.232.143.201;21;Administrator;test;C:\ftp\服务器测试\;E:\wangzgCode\sshLocal\;本地保存测试
     #down;服务IP地址;端口;服务器用户名;服务器密码;服务器文件夹的路径;本地保存地址;本地保存文件名
     ;up;132.232.143.201;21;Administrator;test;E:\wangzgCode\sshLocal;C:\ftp;服务器测试
     #up;服务器IP地址;端口;服务器用户名;服务器密码;本地文件夹的路径;服务器保存地址;服务器保存文件名
     */

    public static void main(String[] args) throws IOException, InterruptedException, SftpException {

//        SSHWindows.scpPut("localhost", "zc9997", "test", "E:/wangzgCode/西北旅游问答.png", "E:/wangzgCode/aa/bb");
//        ConfigBean config = new ConfigBean();
//        config.setAddress("localhost");
//        config.setServerUserName("zc9997");
//        config.setServerPassword("");
//        config.setLocalFilePath("D:\\xyzh\\tool\\apache-maven-3.6.3-bin.zip");
//        config.setServerSavePath("C:\\data");
////
//        SSHWindows.scpPut(config.getAddress(), config.getServerUserName(),
//                config.getServerPassword(), config.getLocalFilePath(),
//                config.getServerSavePath());
//        SSHWindows sshWindows = new SSHWindows();
//        sshWindows.exec2(config, "net stop tomcat8");
////        Thread.sleep(1000*30);
//        sshWindows.exec2(config, "net start tomcat8");

//
//        SSHWindowService sshWindows = new SSHWindowService();
//        sshWindows.connect("127.0.0.1");
//        boolean authenticated = sshWindows.isAuthenticated("zc9997", "jx111111");
//        System.out.println(authenticated);
////        sshWindows.execConmand("cd C:\\tomcat\\apache-tomcat-6.0.41-windows-x64\\apache-tomcat-6.0.41"+"\\bin");
////        sshWindows.execConmand("chcp 65001");
//        // 这句非常重要，开启远程的客户端
//        // 开启后睡眠4秒
//        Thread.sleep(4000);
//        String dir = sshWindows.execConmand("dir");
//        System.out.println(dir);
//        sshWindows.close();


//        String id_rsa111 = Thread.currentThread().getContextClassLoader().getResource("id_rsa_2048-116").getPath();
//        File file = new File(id_rsa111);
//        SSHGanymedUtil sshGanymedUtil = new SSHGanymedUtil("localhost", 22, "zc9997", file);
//        try {
//
//            sshGanymedUtil.loginPuk();
//////            sshGanymedUtil.login();
//            String dir = sshGanymedUtil.execCommand("netstat -ano | findstr 8080");
//            System.out.println(dir);
//            sshGanymedUtil.closeConnection();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

//
        SftpClient sftpClient = new SftpClient();
        sftpClient.setHost("127.0.0.1");
        sftpClient.setUsername("zc9997");
        String id_rsa_2048 = Thread.currentThread().getContextClassLoader().getResource("local").getPath();
        sftpClient.setPrivateKey(id_rsa_2048);
        sftpClient.connect();
        try {
//            sftpClient.getSftp().cd("c:");
            sftpClient.transferDirectory("D:\\temp\\ess","c:\\data\\temp");
        } catch (Exception e) {
            e.printStackTrace();
        }
        sftpClient.disconnect();
//        String id_rsa_2048 = Thread.currentThread().getContextClassLoader().getResource("id_rsa_2048").getPath();
//        SFTPUtil sftpUtil = new SFTPUtil("127.0.0.1","admin", id_rsa_2048);
//        sftpUtil.login();
//        String rm = "C:\\data\\temp";
//
//        sftpUtil.upload(rm.split(Matcher.quoteReplacement("\\"))[0],rm,"ee",new FileInputStream(id_rsa_2048));
//        sftpUtil.logout();


    }
}
